<template>
  <el-dialog title="设置会员" :visible="value" width="700px" custom-class="dialog-box" @close="onClose" @open="onOpen" :close-on-click-modal="false" :append-to-body="true">
    <el-form ref="form" :model="form" :rules="rules" label-width="160px" @submit.native.prevent="onSubmit">
      <el-row>
        <el-col :span="20">
          <el-form-item label="选择会员类型：">
            <el-select v-model="form.levelId" value-key="id">
              <el-option v-for="(item,index) in vipList" :key="index" :label="item.levelEnum.levelName" :value="item.levelEnum.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="20">
          <el-form-item label="会员到期时间：">
            <el-date-picker v-model="form.levelTime" type="date" :picker-options="pickerOptions" value-format="yyyy-MM-dd" placeholder="选择日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-button v-show="false" :loading="btnLoading" native-type="submit">提交</el-button>
    </el-form>
    <span slot="footer">
      <el-button type="primary" :loading="btnLoading" @click="onSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import Valid from "@/_js/validate.js";
export default {
  props: ["value", "curItem"],
  data() {
    return {
      btnLoading: false,
      form: {
        hpId: "",
        patientId: "",
        levelId: "",
        levelTime: ""
      },
      vipList: [],
      rules: {
        levelId: [{ required: true, message: "此项必填", trigger: "change" }],
        levelTime: [{ required: true, message: "此项必填", trigger: "change" }]
      },
      pickerOptions: {
        disabledDate: time => {
          return time.getTime() < Date.now() - 8.64e7;
        }
      },
      typeNameArray: [] //种类标本
    };
  },
  mounted() {
    let th = this;
    th.getBasicData();
  },
  methods: {
    onSubmit() {
      let th = this;
      let j = {};
      let add = function() {
        th.btnLoading = true;
        th.$api.updateLevelEnumList(j).then(res => {
          if (res.executed) {
            th.$message.success("保存成功");
            th.$emit("success");
            th.onClose();
          } else {
            th.$message.error(res.message);
          }
          th.btnLoading = false;
        });
      };
      th.$refs["form"].validate(v => {
        if (v) {
          th.form.hpId = th.adminUser.hpId;
          th.form.patientId = th.curItem.patientId;
          Object.assign(j, th.form);
          add();
        }
      });
    },
    getBasicData() {
      let th = this;
      th.$api
        .LevelList({ hpId: th.adminUser.hpId, pageSize: "60000", pageNum: "1" })
        .then(res => {
          if (res.executed) {
            th.vipList = res.levelEnumInfoResponses;
          }
        });
    },
    onOpen() {
      let th = this;
      th.$nextTick(function() {
        if (th.curItem) {
          th.form.hpId = th.adminUser.hpId;
          th.form.levelId = th.curItem.levelId;
          th.form.patientId = th.curItem.patientId;
          th.form.levelTime = "";
        }
      });
    },
    onClose() {
      let th = this;
      th.$refs["form"].resetFields();
      th.$emit("input", false);
    }
  }
};
</script>
